from langchain import OpenAI, SerpAPIWrapper
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI

import os

os.environ["SERPAPI_API_KEY"] = '1c39b1eff5ecf89c020b71250356b26ac50d8da1cbf0aa20e48d2445e6803ab9'
os.environ["OPENAI_API_KEY"] = 'sk-f2M0jWxDEb7IJKiuCnMsvKE7TLSYCA36pyl8gWA1XLaHnD8l'
os.environ["OPENAI_BASE_URL"] = 'https://api.f2gpt.com/v1'

llm = ChatOpenAI(
    model="gpt-3.5-turbo",
    temperature=0)

search = SerpAPIWrapper()
tools = [
    Tool(
        name="Intermediate Answer",
        func=search.run,
        description="useful for when you need to ask with search",
    )
]

self_ask_with_search = initialize_agent(
    tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True, handle_parsing_errors=True
)
answer = self_ask_with_search.run(
    "使用玫瑰作为国花的国家的首都是哪里"
)

print(answer)
